package net.edu_soft.ec.asset.service.impl;

import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.SysLogMapper;
import net.edu_soft.ec.asset.pojo.Permission;
import net.edu_soft.ec.asset.pojo.SysLog;
import net.edu_soft.ec.asset.pojo.SysLogExample;
import net.edu_soft.ec.asset.service.SysLogService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.openmbean.OpenDataException;
import java.util.Date;
import java.util.List;


@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public void save(SysLog sysLog) {
        System.out.println("保存数据--------》" + sysLog);
        sysLogMapper.insertSelective(sysLog);
    }

    @Override
    public PageInfo<SysLog> find(SysLog sysLog, Integer sort, Integer upOrDown, Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("查询日志：分页参数不能为0或者小于等于0", pageNum, pageSize);
        SysLogExample example = new SysLogExample();
        SysLogExample.Criteria criteria = example.createCriteria();
        if (sysLog.getUserName() != null && !"".equals(sysLog.getUserName())) {
            criteria.andUserNameLike("%" + sysLog.getUserName() + "%");
        }
        if (sysLog.getOperation() != null && !"".equals(sysLog.getOperation())) {
            criteria.andOperationLike("%" + sysLog.getOperation() + "%");
        }
        //制定日期上还是下
        if (upOrDown != null && upOrDown == 1 && sysLog.getCreateDate() != null) {
            criteria.andCreateDateGreaterThan(sysLog.getCreateDate());

        } else if (upOrDown != null && upOrDown == 2 && sysLog.getCreateDate() != null) {
            criteria.andCreateDateLessThan(sysLog.getCreateDate());

        }
        //排序
        if (sort != null && sort == 1) {
            example.setOrderByClause("create_date desc");
        } else if (sort != null && sort == 2) {
            example.setOrderByClause("create_date asc");
        }

		System.out.println("sort: "+sort+" , upOrDown:  "+upOrDown);

        List<SysLog> sysLogs = sysLogMapper.selectByExample(example);
        PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogs);
        return pageInfo;
    }

    @Override
    public int del(String ids, Integer isDel) {
        int i = 0;
        //删除
        if (isDel == 1 && isDel != null) {
            //提取id
            List<Integer> list = PuingUtil.getNumber(ids);
            //循环查询
            for (Integer id : list) {
                //查询是否存在
                SysLog sysLog = sysLogMapper.selectByPrimaryKey(id);
                if (sysLog == null) {
                    throw new ParameterException("删除日志：日志信息不能为空,id: " + id);
                }
                i = sysLogMapper.deleteByPrimaryKey(id);
            }
        }
        return i;
    }

}
